var app = getApp(),
  dataApi = require("../../provider/dataApi.js");

require("../../provider/pipe.js");

Page({
  data: {
    statusBarHeight: "",
    titleBarHeight: "",
    oItem: {},
    isData: !0,
    showPay: !1,
    price: "",
    vipPrice: ""
  },
  onLoad: function (params) {
    if (wx.getStorageSync("uid")) {
      var deviceModel = wx.getSystemInfoSync().model.slice(0, 6),
        deviceBrand = wx.getSystemInfoSync().model.slice(0, 4);
      params = !1, "iPhone" != deviceModel && "iPad" != deviceBrand || (params = !0), deviceModel = app.globalData.oItem, this.setData({
        statusBarHeight: app.globalData.statusBarHeight,
        titleBarHeight: app.globalData.titleBarHeight,
        oItem: deviceModel,
        isIos: params
      });
    } else wx.navigateTo({
      url: "/pages/login/login"
    });
  },
  getKnowledge: function (item) {
    var self = this;
    dataApi.knowledge({
      op: "knowledgelist",
      uid: wx.getStorageSync("uid"),
      cate: item.id
    }).then(function (response) {
      console.log(response);
      var hasData = !1;
      0 < response.data.list.length && (hasData = !0), self.setData({
        list: response.data.list,
        price: response.data.price,
        vipPrice: response.data.vip_price,
        payOpen: response.data.pay_open,
        isData: hasData
      });
    }).catch(function (error) {
      console.log(error);
    });
  },
  getUserInfo: function () {
    var self = this;
    dataApi.userinfo({
      uid: wx.getStorageSync("uid"),
      op: "getinfo"
    }).then(function (response) {
      console.log(response), self.setData({
        userInfo: response.data.info
      });
    }).catch(function (error) {
      console.log(error);
    });
  },
  goBack: function () {
    wx.navigateBack({
      delta: 1
    });
  },
  closePayPopup: function () {
    this.setData({
      showPay: !1
    });
  },
  goLook: function (event) {
    event = event.currentTarget.dataset.item;
    if (1 != this.data.oItem.fatherIsCan) {
        this.data.isIos && 2 == this.data.userInfo.IOS ? this.iosTip() : this.setData({
            showPay: !0
        });
    } else {
        app.globalData.oItem = event;
        wx.navigateTo({
            url: "../knowledgePoints-list-detail/knowledgePoints-list-detail"
        });
    }
  },
  iosTip: function () {
    wx.showModal({
      cancelText: "关闭",
      confirmText: "联系客服",
      content: "由于相关规范，ios功能暂不可用，如有问题请联系客服",
      showCancel: !0,
      title: "提示",
      success: function (result) {
        result.confirm && wx.navigateTo({
          url: "/pages/contactService/contactService"
        });
      }
    });
  },
  goVipBtn: function () {
    wx.navigateTo({
      url: "../vip/vip"
    });
  },
  goPayBtn: function () {
    this.pay();
  },
  pay: function () {
    var self = this,
      item = this.data.oItem;
    dataApi.wxPay({
      uid: wx.getStorageSync("uid"),
      id: item.chapterId,
      type: "2"
    }).then(function (response) {
      console.log(response), wx.requestPayment({
        timeStamp: String(response.data.timeStamp),
        nonceStr: response.data.nonceStr,
        package: response.data.package,
        signType: response.data.signType,
        paySign: response.data.paySign,
        success: function (result) {
          wx.showToast({
            icon: "success",
            title: "支付成功"
          }), self.setData({
            showPay: !1
          }), self.getKnowledge(self.data.oItem);
        },
        fail: function (error) {
          wx.showToast({
            icon: "none",
            title: "支付失败,请重试~"
          });
        }
      });
    }).catch(function (error) {
      console.log(error);
    });
  },
  onShow: function () {
    this.getUserInfo(), this.getKnowledge(this.data.oItem);
  },
  onShareAppMessage: function () {}
});